package com.ruoyi.web.controller.system;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.system.domain.vo.ACreateResultVo;
import com.ruoyi.system.domain.vo.ARuKuVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AResult;
import com.ruoyi.system.service.IAResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 追溯结果Controller
 * 
 * @author ruoyi
 * @date 2025-03-16
 */
@RestController
@RequestMapping("/system/aResult")
public class AResultController extends BaseController
{

    @Autowired
    private IAResultService aResultService;

    /**
     * 查询追溯结果列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AResult aResult)
    {
        startPage();
        List<AResult> list = aResultService.selectAResultList(aResult);
        return getDataTable(list);
    }

    /**
     * 查询追溯结果列表
     */
    @GetMapping("/retraceResultList")
    public TableDataInfo retraceResultList(AResult aResult)
    {
        startPage();
        HashMap resMap = aResultService.retraceResultList(aResult);
        List<LinkedHashMap> list = (List) resMap.get("resList");
        List<AResult> packList = (List) resMap.get("packList");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(packList).getTotal());
        return rspData;
    }

    /**
     * 导出追溯结果列表
     */
    @Log(title = "追溯结果", businessType = BusinessType.EXPORT)
    @PostMapping("/exportResult")
    public void exportResult(HttpServletResponse response, AResult aResult)
    {
        aResultService.exportResult(response, aResult);
    }

    /**
     * 导出追溯结果列表
     */
    @PreAuthorize("@ss.hasPermi('system:aResult:export')")
    @Log(title = "追溯结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AResult aResult)
    {
        List<AResult> list = aResultService.selectAResultList(aResult);
        ExcelUtil<AResult> util = new ExcelUtil<AResult>(AResult.class);
        util.exportExcel(response, list, "追溯结果数据");
    }

    /**
     * 获取追溯结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:aResult:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aResultService.selectAResultById(id));
    }

    /**
     * 新增追溯结果
     */
    @PreAuthorize("@ss.hasPermi('system:aResult:add')")
    @Log(title = "追溯结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AResult aResult)
    {
        return toAjax(aResultService.insertAResult(aResult));
    }

    /**
     * 修改追溯结果
     */
    @PreAuthorize("@ss.hasPermi('system:aResult:edit')")
    @Log(title = "追溯结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AResult aResult)
    {
        return toAjax(aResultService.updateAResult(aResult));
    }

    /**
     * 删除追溯结果
     */
    @PreAuthorize("@ss.hasPermi('system:aResult:remove')")
    @Log(title = "追溯结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aResultService.deleteAResultByIds(ids));
    }

    /**
     * 批次生成模板下载
     * @param response
     */
    @PostMapping("/downloadRetraceResultTemplate")
    public void downloadRetraceResultTemplate(HttpServletResponse response, ACreateResultVo resultVo)
    {
        List<ACreateResultVo> list = new ArrayList<>();
        ExcelUtil<ACreateResultVo> util = new ExcelUtil<ACreateResultVo>(ACreateResultVo.class);
        list.add(new ACreateResultVo("WORK_ORDER_0001", "IN_BATCH_NO_0001", "PALLET_NO_0001", "BOX_NO_0001", "25.30%", "L3", "L", "847", "A", "PACK0001", 150, new Date()));
        list.add(new ACreateResultVo("WORK_ORDER_0002", "IN_BATCH_NO_0002", "PALLET_NO_0002", "BOX_NO_0002", "25.30%", "L3", "L", "847", "A", "PACK0001", 150, new Date()));
        util.exportExcel(response, list, "生成批次模板");
    }

    /**
     * 新增追溯结果
     */
    @Log(title = "追溯结果", businessType = BusinessType.INSERT)
    @PostMapping("/createResult")
    public AjaxResult createResult(@RequestBody JSONObject jo) throws Exception
    {
        return aResultService.createResult(jo);
    }

    @GetMapping("/routeList")
    public AjaxResult routeList()
    {
        return AjaxResult.success(aResultService.routeList());
    }

    /**
     * 入库查询列表
     */
    @GetMapping("/selectRuKuList")
    public TableDataInfo selectRuKuList(ARuKuVo ruKuVo) {
        startPage();
        List<ARuKuVo> list = aResultService.selectRuKuList(ruKuVo.getInBatchNo());
        return getDataTable(list);
    }

    /**
     * 导出入库记录列表
     */
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRuKu")
    public void export(HttpServletResponse response, ARuKuVo ruKuVo) {
        List<ARuKuVo> list = aResultService.selectRuKuList(ruKuVo.getInBatchNo());
        ExcelUtil<ARuKuVo> util = new ExcelUtil<ARuKuVo>(ARuKuVo.class);
        util.exportExcel(response, list, "入库记录数据");
    }

    /**
     * 工单剩余数量查询
     */
    @GetMapping("/getWorkOrderRestQty")
    public AjaxResult getWorkOrderRestQty(@RequestParam String workOrder)
    {
        return aResultService.getWorkOrderRestQty(workOrder);
    }

}
